package Impl;

import dao.IBoardDao;
import entity.Board;
import until.DBuntil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BoardDaoImpl extends DBuntil implements IBoardDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public int addBoard(String boardName) {
        conn=getConn();
        int n=0;
        try {
            String sql="INSERT INTO TB_BOARD (COL_BNAME,COL_BDEL) VALUES(?,1)";
            pst=conn.prepareStatement(sql);
            pst.setString(1,boardName);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cloAll(conn,pst,null);
        }
        return n;
    }

    @Override
    public int delBoard(int boardId) {

        conn=getConn();
        int n=0;
        try {
            String sql="UPDATE TB_BOARD SET COL_BDEL=-1 WHERE COL_BID=?";
            pst=conn.prepareStatement(sql);
            pst.setInt(1,boardId);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cloAll(conn,pst,null);
        }
        return n;
    }

    @Override
    public int updateBoard(String boardName,int boardId) {
        conn=getConn();
        int n=0;
        try {
            String sql="UPDATE TB_BOARD SET COL_BNAME=? WHERE COL_BID=? AND COL_BDEL=1";
            pst=conn.prepareStatement(sql);
            pst.setString(1,boardName);
            pst.setInt(2,boardId);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cloAll(conn,pst,null);
        }
        return n;
    }

    @Override
    public List showAll() {
        List list=new ArrayList();
        conn=getConn();
        try {
            String sql="SELECT * FROM TB_BOARD WHERE COL_BDEL=1";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            while(rs.next()){
                Board board=new Board();
                board.setBoardId(rs.getInt(1));
                board.setBoardName(rs.getString(2));
                board.setBoardDel(rs.getInt(3));
                list.add(board);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cloAll(conn,pst,rs);
        }
        return list;
    }

    @Override
    public List keyShowAll(String boardName) {
        List list=new ArrayList();
        conn=getConn();
        try {
            String sql="SELECT * FROM TB_BOARD WHERE COL_BDEL=1 AND COL_BNAME LIKE ? ";
            pst=conn.prepareStatement(sql);
            pst.setString(1,"%"+boardName+"%");
            rs=pst.executeQuery();
            while (rs.next()){
                Board board=new Board();
                board.setBoardId(rs.getInt(1));
                board.setBoardName(rs.getString(2));
                board.setBoardDel(rs.getInt(3));
                list.add(board);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cloAll(conn,pst,rs);
        }
        return list;
    }

    @Override
    public Board getBoardId(int boardId) {
        Board board=null;
        conn=getConn();

        try {
            String sql="SELECT * FROM TB_BOARD WHERE COL_BID=?";
            pst=conn.prepareStatement(sql);
            pst.setInt(1,boardId);
            rs=pst.executeQuery();
            while (rs.next()){
                board=new Board();
                board.setBoardId(rs.getInt(1));
                board.setBoardName(rs.getString(2));
                board.setBoardDel(rs.getInt(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return board;
    }
}
